package com.alks.function.controller.procurement;


import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.Idempotent;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.procurement.PurchaseOrderDto;
import com.alks.function.data.request.procurement.*;
import com.alks.function.mapper.procurement.SummaryOperationMapper;
import com.alks.function.service.procurement.MaterialClearanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static java.lang.Thread.sleep;


@ApiRestController("/MaterialClearance")
@Slf4j
@Api(tags = {"采购部-材料请购作业-材料查询","采购部-材料请购作业-进入订购程序"})
@RoleCheck(prgIds = {"150102"})
public class MaterialClearanceOperationController {

    @Autowired
    private MaterialClearanceService materialClearanceService;

    @WebLogger
    @GetMapping("/getAllLotNoList")
    @ApiOperation(value = "指令条件查询",tags = "采购部-材料请购作业-材料查询")
    public ResponseInfo getAllLotNoList(@RequestParam(required = false) String lotNo){
        return materialClearanceService.getAllLotNoList(lotNo);
    }

    @WebLogger
    @PostMapping("/getLotNoList")
    @ApiOperation(value = "指令明细查询",tags = "采购部-材料请购作业-材料查询")
    public ResponseInfo getLotNoList(@RequestBody LotNoRequest request){
        return materialClearanceService.getLotNoList(request);
    }

    @WebLogger
    @PostMapping("/getSummaryMaterialsList")
    @ApiOperation(value = "汇总材料查询",tags = "采购部-材料请购作业-材料查询")
    public ResponseInfo getSummaryMaterialsList(@RequestBody LotNoRequest2 request){
        return materialClearanceService.getSummaryMaterialsList(request);
    }

    @WebLogger
    @PostMapping("/getBatchProcess")
    @ApiOperation(value = "请求批处理",tags = "采购部-材料请购作业-进入订购程序")
    //@Idempotent(expireTime = 5,info = "系统繁忙，请稍后刷新后再试")
    @Idempotent(
            key = "'MC_getBatchProcess_' + #request.![lotNo + '_' + type + '_' + mtlColor + '_' + mtlName + '_' + mtlSpec + '_' + mtlUnit].stream().sorted().collect(T(java.util.stream.Collectors).joining(','))",
            expireTime = 5,
            info = "请勿重复提交，请检查是否已重复处理过相同材料，如已处理，请刷新后再试"
    )
    public ResponseInfo getBatchProcess(@RequestBody List<LotNoRequest5> request){
        return materialClearanceService.getBatchProcess(request);
    }

    @WebLogger
    @PostMapping("/getPurchaseOrderList")
    @ApiOperation(value = "订购单查询",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo getPurchaseOrderList(@RequestBody PurchaseOrderRequest request){
        return materialClearanceService.getPurchaseOrderList(request);
    }

    @WebLogger
    @PostMapping("/getAllPurchaseOrderList")
    @ApiOperation(value = "所有订购单查询",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo getAllPurchaseOrderList(@RequestBody PurchaseOrderRequest request){
        return materialClearanceService.getAllPurchaseOrderList(request);
    }

    @WebLogger
    @PostMapping("/updatePurchaseOrder")
    @ApiOperation(value = "我的订购单：编辑",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo updatePurchaseOrder(@RequestBody List<PurchaseOrderDto> list){
        return materialClearanceService.updatePurchaseOrder(list);
    }

    @WebLogger
    @PostMapping("/updatePurchaseOrder2")
    @ApiOperation(value = "我的订购单：单条编辑",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo updatePurchaseOrder2(@RequestBody PurchaseOrderDto request){
        return materialClearanceService.updatePurchaseOrder2(request);
    }

    @WebLogger
    @PostMapping("/updatePurchaseOrderBatch")
    @ApiOperation(value = "我的订购单：批量改厂商",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo updatePurchaseOrderBatch(@RequestBody PurchaseOrderRequest2 request){
        return materialClearanceService.updatePurchaseOrderBatch(request);
    }

    @WebLogger
    @PostMapping("/makePurchaseOrder")
    @ApiOperation(value = "形成订购单",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo makePurchaseOrder(@RequestBody LotNoRequest4  r){
        return materialClearanceService.makePurchaseOrder(r);
    }

    @WebLogger
    @PostMapping("/sendBack")
    @ApiOperation(value = "退回（未订购订单到批处理）",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo sendBack(@RequestBody LotNoRequest6 request){
        return materialClearanceService.sendBack(request);
    }

    @WebLogger
    @PostMapping("/sendBackBatch")
    @ApiOperation(value = "批量退回所有订购单（批量任何订单退到批处理）",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo sendBackBatch(@RequestBody List<LotNoRequest6> request){
        return materialClearanceService.sendBackBatch(request);
    }

    @WebLogger
    @PostMapping("/backPurchaseOrder")
    @ApiOperation(value = "单行取消订购单（已订购订单到批处理）",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo backPurchaseOrder(@RequestBody LotNoRequest6 request){
        return materialClearanceService.backPurchaseOrder(request);
    }

//    @WebLogger
//    @PostMapping("/backPurchaseOrderBatch")
//    @ApiOperation(value = "单行取消订购单（已订购订单到批处理）-批量",tags = "采购部-材料请购作业-进入订购程序")
//    public ResponseInfo backPurchaseOrderBatch(@RequestBody List<LotNoRequest6> request){
//        return materialClearanceService.backPurchaseOrderBatch(request);
//    }

    @WebLogger
    @PostMapping("/cancelPurchaseOrder")
    @ApiOperation(value = "置空订购信息（已订购订单取消订购）",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo cancelPurchaseOrder(@RequestBody LotNoRequest6 request){
        return materialClearanceService.cancelPurchaseOrder(request);
    }
    
    @WebLogger
    @PostMapping("/cancelPurchaseOrderNumber")
    @ApiOperation(value = "订购取消:1.取消处理（已订购订单订单数量更改（只能变少））2.取消还原处理",tags = "采购部-材料请购作业-进入订购程序")
    public ResponseInfo cancelPurchaseOrderNumber(@RequestBody LotNoRequest7 request){
        return materialClearanceService.cancelPurchaseOrderNumber(request);
    }

    @WebLogger
    @GetMapping("/exportPurchaseOrder")
    @ApiOperation(value = "导出订购单",tags = "采购部-材料请购作业-进入订购程序")
    public void exportPurchaseOrder(@RequestParam String purNo, HttpServletResponse response){
         materialClearanceService.exportPurchaseOrder(purNo,response);
    }

    @WebLogger
    @GetMapping("/exportSziePurchaseOrder")
    @ApiOperation(value = "导出分码订购单",tags = "采购部-材料请购作业-进入订购程序")
    public void exportSziePurchaseOrder(@RequestParam String purNo, HttpServletResponse response){
        materialClearanceService.exportSziePurchaseOrder(purNo,response);
    }

}
